var t = function(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}(require("../../utils/config.js")), e = require("../../utils/api.js"), o = (require("../../utils/util.js"), 
require("../../utils/auth.js")), i = (require("../../wxParse/wxParse.js"), require("../../utils/wxApi.js")), n = require("../../utils/wxRequest.js"), s = getApp();

Page({
    data: {
        text: "Page topic",
        categoriesList: {},
        floatDisplay: "none"
    },
    onLoad: function(e) {
        wx.setNavigationBarTitle({
            title: t.default.getWebsiteName + " - 专题",
            success: function(t) {}
        }), this.fetchCategoriesData();
    },
    onShow: function(t) {},
    fetchCategoriesData: function() {
        var t = this;
        t.setData({
            categoriesList: []
        }), n.getRequest(e.getCategories()).then(function(e) {
            200 === e.statusCode ? t.setData({
                floatDisplay: "block",
                categoriesList: t.data.categoriesList.concat(e.data.map(function(t) {
                    return void 0 !== t.category_thumbnail_image && "" != t.category_thumbnail_image || (t.category_thumbnail_image = "../../images/website.png"), 
                    t.subimg = "subscription.png", t;
                }))
            }) : console.log(e);
        }).then(function(e) {
            s.globalData.isGetOpenid && setTimeout(function() {
                t.getSubscription();
            }, 500);
        }).catch(function(t) {
            console.log(t);
        }).finally(function() {});
    },
    onShareAppMessage: function() {
        return {
            title: "分享“" + t.default.getWebsiteName + "”的专题栏目.",
            path: "pages/topic/topic",
            success: function(t) {},
            fail: function(t) {}
        };
    },
    getSubscription: function() {
        var t = this;
        if (wx.showLoading({
            title: "正在加载",
            mask: !0
        }), s.globalData.isGetOpenid) {
            var o = e.getSubscription() + "?openid=" + s.globalData.openid;
            n.getRequest(o).then(function(e) {
                if ("200" == e.data.status) {
                    var o = e.data.subscription, i = t.data.categoriesList, n = [];
                    if (o && i) {
                        for (var s = 0; s < i.length; s++) {
                            for (var a = "subscription.png", u = "0", c = 0; c < o.length; c++) {
                                i[s].id == o[c] && (a = "subscription-on.png", u = "1");
                                var r = "";
                                r = void 0 === i[s].category_thumbnail_image || "" == i[s].category_thumbnail_image ? "../../images/website.png" : i[s].category_thumbnail_image;
                            }
                            var l = {
                                category_thumbnail_image: r,
                                description: i[s].description,
                                name: i[s].name,
                                id: i[s].id,
                                subimg: a,
                                subflag: u
                            };
                            n.push(l);
                        }
                        n.length > 0 && t.setData({
                            floatDisplay: "block",
                            categoriesList: n
                        });
                    }
                } else console.log(e);
            }).finally(function() {
                setTimeout(function() {
                    wx.hideLoading();
                }, 500), wx.hideNavigationBarLoading();
            });
        }
    },
    postsub: function(t) {
        var o = this;
        if (s.globalData.isGetOpenid) {
            var i = t.currentTarget.dataset.id, a = s.globalData.openid, u = e.postSubscription(), c = t.currentTarget.dataset.subflag, r = {
                categoryid: i,
                openid: a
            };
            n.postRequest(u, r).then(function(t) {
                if (200 === t.statusCode) if ("200" == t.data.status) {
                    setTimeout(function() {
                        wx.showToast({
                            title: "订阅成功",
                            icon: "success",
                            duration: 900,
                            success: function() {}
                        });
                    }, 900);
                    e = "";
                    "0" == c ? (c = "1", e = "subscription-on.png") : (c = "0", e = "subscription.png"), 
                    o.reloadData(i, c, e);
                } else if ("201" == t.data.status) {
                    setTimeout(function() {
                        wx.showToast({
                            title: "取消订阅成功",
                            icon: "success",
                            duration: 900,
                            success: function() {}
                        });
                    }, 900);
                    var e = "";
                    "0" == c ? (c = "1", e = "subscription-on.png") : (c = "0", e = "subscription.png"), 
                    o.reloadData(i, c, e);
                } else "501" != t.data.status && "501" != t.data.status || console.log(t); else setTimeout(function() {
                    wx.showToast({
                        title: "操作失败,请稍后重试",
                        icon: "success",
                        duration: 900,
                        success: function() {}
                    });
                }, 900), console.log(t);
            }).catch(function(t) {
                setTimeout(function() {
                    wx.showToast({
                        title: "操作失败,请稍后重试",
                        icon: "success",
                        duration: 900,
                        success: function() {}
                    });
                }, 900), console.log(t);
            });
        } else o.userAuthorization();
    },
    reloadData: function(t, e, o) {
        for (var i = this, n = [], s = i.data.categoriesList, a = 0; a < s.length; a++) s[a].id == t && (s[a].subflag = e, 
        s[a].subimg = o), n.push(s[a]);
        n.length > 0 && i.setData({
            categoriesList: n
        });
    },
    redictIndex: function(t) {
        var e = t.currentTarget.dataset.id, o = (t.currentTarget.dataset.item, "../list/list?categoryID=" + e);
        wx.navigateTo({
            url: o
        });
    },
    userAuthorization: function() {
        var t = this;
        wx.getSetting({
            success: function(e) {
                console.log(e.authSetting);
                var i = e.authSetting;
                "scope.userInfo" in i ? (console.log("不是第一次授权", i), !1 === i["scope.userInfo"] ? wx.showModal({
                    title: "用户未授权",
                    content: "如需正常使用评论、点赞等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?",
                    showCancel: !0,
                    cancelColor: "#296fd0",
                    confirmColor: "#296fd0",
                    confirmText: "设置权限",
                    success: function(e) {
                        e.confirm && (console.log("用户点击确定"), wx.openSetting({
                            success: function(e) {
                                console.log("打开设置", e.authSetting), e.authSetting["scope.userInfo"] && t.getUsreInfo(null);
                            }
                        }));
                    }
                }) : o.getUsreInfo(null)) : (console.log("第一次授权"), t.setData({
                    isLoginPopup: !0
                }));
            }
        });
    },
    agreeGetUser: function(t) {
        var e = t.detail.userInfo, i = this;
        e && (o.getUsreInfo(t.detail), i.setData({
            userInfo: e
        })), setTimeout(function() {
            i.setData({
                isLoginPopup: !1
            });
        }, 1200);
    },
    closeLoginPopup: function() {
        this.setData({
            isLoginPopup: !1
        });
    },
    openLoginPopup: function() {
        this.setData({
            isLoginPopup: !0
        });
    },
    getUsreInfo: function(t) {
        var e = this, o = "";
        i.wxLogin()().then(function(e) {
            return o = e.code, null == t ? i.wxGetUserInfo()() : t;
        }).then(function(t) {
            console.log(t.userInfo), console.log("成功获取用户信息(公开信息)"), s.globalData.userInfo = t.userInfo, 
            s.globalData.isGetUserInfo = !0;
            var i = {
                js_code: o,
                encryptedData: t.encryptedData,
                iv: t.iv,
                avatarUrl: t.userInfo.avatarUrl,
                nickname: t.userInfo.nickName
            };
            e.getOpenId(i);
        }).catch(function(t) {
            console.log("error: " + t.errMsg);
        });
    },
    getOpenId: function(t) {
        var o = e.getOpenidUrl(), i = this;
        n.postRequest(o, t).then(function(t) {
            "200" == t.data.status ? (console.log("openid 获取成功"), s.globalData.openid = t.data.openid, 
            s.globalData.isGetOpenid = !0) : console.log(t);
        }).then(function(t) {
            setTimeout(function() {
                i.getSubscription();
            }, 500);
        });
    },
    confirm: function() {
        this.setData({
            "dialog.hidden": !0,
            "dialog.title": "",
            "dialog.content": ""
        });
    }
});